#include "propcoordinatedialog.h"
#include "ui_propcoordinatedialog.h"
#include <QMouseEvent>
#include <QDebug>

PropCoordinateDialog::PropCoordinateDialog(PropData *m, double lat, double lon, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PropCoordinateDialog)
{
    ui->setupUi(this);

    map = m;
    ui->latitudeDoubleSpinBox->setValue(lat);
    ui->longitudeDoubleSpinBox->setValue(lon);

    scene = new PropCoordinateDialogScene;
    connect(scene, SIGNAL(mouseClickCoords(double,double)), this, SLOT(setSelectedCoords(double,double)));
    mapimg = new QPixmap(map->pic[0]->first);
    scene->setMapImagePixmap(mapimg);
    ui->mapGraphicsView->setScene(scene);

    setWindowFlags(Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);
    setWindowState(Qt::WindowMaximized);
}

PropCoordinateDialog::~PropCoordinateDialog()
{
    delete ui;
}

void PropCoordinateDialog::setSelectedCoords(double x, double y)
{
    double lat = ((y * (map->propCoords[1].latitude - map->propCoords[0].latitude)) / mapimg->height()) + map->propCoords[0].latitude;
    double lon = ((x * (map->propCoords[1].longitude - map->propCoords[0].longitude)) / mapimg->width()) + map->propCoords[0].longitude;

    ui->latitudeDoubleSpinBox->setValue(lat);
    ui->longitudeDoubleSpinBox->setValue(lon);

    emit newLatLon(lat, lon);
}

/*void PropCoordinateDialog::mousePressEvent(QMouseEvent *mEvent)
{
    qDebug() << mEvent->x();
    qDebug() << mEvent->y();
    emit logMessage("mouse pressed", IMPORTANT);
}*/

void PropCoordinateDialog::on_closePushButton_clicked()
{
    accept();
}
